package com.veryUsedCar.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.veryUsedCar.data.Result;
import com.veryUsedCar.enums.ConstantEnum;
import com.veryUsedCar.po.User;
import com.veryUsedCar.service.UserService;
import com.veryUsedCar.utils.JwtUtil;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user/v1")
@Slf4j
@Api(value = "用户接口")
@RefreshScope //刷新配置
public class UserController {

   @Autowired
   private UserService userService;

   @Autowired
   private JwtUtil jwtUtil;
   /*@Value("${user.headimage.url}")
   private String url;*/
    //登录
    @GetMapping("/findUserByName")
    @ApiOperation(value = "根据uname查询用户")
    @ApiImplicitParam(name = "uname",value = "用户实体",required = true, paramType = "query")
    public Result findUserByName(@RequestParam String uname){
        log.info("login方法");
        User user = userService.findUserByName(uname);
        return Result.OK().setData(user);
    }
    @PostMapping("/token")
    @ApiOperation(value = "获取登录token")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token",value = "token",required = true, paramType = "header")
    })
    public Result getToken(@RequestHeader("token") String token){
        Claims claims = jwtUtil.parseJwt(token);
        String subject = claims.getSubject();
        //Long aLong = Long.valueOf(claims.getId());
        log.info("获取登录token{}:",subject);
        return Result.OK();
    }

    @PostMapping
    @ApiOperation(value = "新增用户")
    @ApiImplicitParam(name = "user",value = "用户实体",required = true, paramType = "body")
    public Result addUser(@RequestBody User user){
        log.info("增加方法");
        userService.addUser(user);
        return Result.OK();
    }

    //修改
    @PutMapping
    @ApiOperation(value = "修改用户")
    @ApiImplicitParam(name = "user",value = "用户实体",required = true, paramType = "body")
    public Result updateUser(@RequestBody User user){

        userService.updateUser(user);
        return Result.OK();
    }

    //根据id查询
    @GetMapping ("/{uid}")//restful注解
    @ApiOperation(value = "根据uid查询")
    @ApiImplicitParam(name = "uid", value = "根据id查询",required = true,paramType = "query")
    public Result findById(@PathVariable Long uid){ //@RequestParam 以json传入
        User user = userService.findById(uid);
        return Result.OK().setData(user);
    }

    //根据id删除
    @DeleteMapping
    @ApiOperation(value = "根据uid删除")
    @ApiImplicitParam(name = "uid", value = "根据id删除",required = true,paramType = "query")
    public Result deleteById(@RequestParam Long uid){
        userService.deleteById(uid);
        return Result.OK();
    }

    @GetMapping("/getUserHeadImage")
    @SentinelResource(value = "getUserHeadImage", blockHandler = "flowLimiting")
    @ApiOperation(value = "获取头像地址")
    public Result getUserHeadImage(){

        return Result.OK();
    }

    //为getUserHeadImage方法处理异常
    public Result flowLimiting(BlockException e){

        return Result.build(ConstantEnum.FLOWLIMITING);
    }
}
